package com.uphone.magicleafteacher.pro.activity.courselivecreate;

import android.arch.lifecycle.LifecycleOwner;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.base.mvp.BasePAV;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RequestBodyUtil;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.vo.CourseLiveListEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import javax.inject.Inject;

import okhttp3.MultipartBody;

/**
 * Created by hzy on 2019/1/18
 * WelcomePresenter
 *
 * @author hzy
 */
public class CourseLiveCreatePresenter extends BasePAV<CourseLiveCreateContract.View> implements CourseLiveCreateContract.Presenter {

    @Inject
    CourseLiveCreatePresenter() {

    }

    public void uploadImgFile(File file, String courseName, String beginTime, String endTime, String prepare, int levelId, int classId) {
        MultipartBody.Part part = RequestBodyUtil.filesToMultipartBodyPart("file", file);
        MyApp.apiService(ApiService.class)
                .uploadImgFile(part)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        String filePath = SuccessfulConsumer.getKey(jsonObject, "filePath");
                        if (!TextUtils.isEmpty(filePath)) {
                            createLiveClass(courseName, beginTime, endTime, prepare, levelId, classId, filePath);
                        } else {
                            ToastUtils.showShort("上传封面失败");
                        }
                    }
                }, new FailureConsumer());

    }

    public void createLiveClass(String courseName, String beginTime, String endTime, String prepare, int levelId, int classId, String fileUrl) {

        SharedPreferences preferences = MyApp.getInstance().getSharedPreferences("moye", MyApp.getInstance().MODE_PRIVATE);
        String teacherId = preferences.getString("teacherId", "");
        MyApp.apiService(ApiService.class)
                .createLiveClass(teacherId, courseName, beginTime, endTime, prepare, levelId + "", classId + "", fileUrl)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        EventBus.getDefault().post(new CourseLiveListEvent());
                        CommonUtil.quitActivity(mView.getBaseActivity(), "创建成功");
                    }
                }, new FailureConsumer());

    }

    public void uploadImgFile(File file, int liveId, int classSelectBackId, int rankSelectBackId) {
        MultipartBody.Part part = RequestBodyUtil.filesToMultipartBodyPart("file", file);
        MyApp.apiService(ApiService.class)
                .uploadImgFile(part)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        String filePath = SuccessfulConsumer.getKey(jsonObject, "filePath");
                        if (!TextUtils.isEmpty(filePath)) {
                            editLiveClass(liveId, classSelectBackId, rankSelectBackId, filePath);
                        } else {
                            ToastUtils.showShort("上传封面失败");
                        }
                    }
                }, new FailureConsumer());

    }

    public void editLiveClass(int liveId, int classSelectBackId, int rankSelectBackId, String facePic) {
        SharedPreferences preferences = MyApp.getInstance().getSharedPreferences("moye", MyApp.getInstance().MODE_PRIVATE);
        String teacherId = preferences.getString("teacherId", "");
        MyApp.apiService(ApiService.class)
                .editLiveClass(teacherId, liveId + "", classSelectBackId + "", rankSelectBackId + "", facePic)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        EventBus.getDefault().post(new CourseLiveListEvent());
                        CommonUtil.quitActivity(mView.getBaseActivity(), "编辑成功");
                    }
                }, new FailureConsumer());


    }

    public void liveClassInfo(int liveId) {
        MyApp.apiService(ApiService.class)
                .liveClassInfo(liveId + "")
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        mView.liveClassInfo(jsonObject);
                    }
                }, new FailureConsumer());

    }

    public void delLiveClass(int liveId) {
        MyApp.apiService(ApiService.class)
                .delLiveClass(CommonUtil.getTeacherId(), liveId + "")
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        EventBus.getDefault().post(new CourseLiveListEvent());
                        CommonUtil.quitActivity(mView.getBaseActivity(), "删除成功");
                    }
                }, new FailureConsumer());

    }
}
